<template>
  <div>
    <el-dialog title="分配角色" :visible="showRoleDialog" @close="closeFn">
      <el-form>
        <el-form-item>
          <el-checkbox-group v-model="checkList">
            <el-checkbox
              v-for="item in roleList"
              :key="item.id"
              :label="item.id"
            >
              {{ item.name }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeFn">取 消</el-button>
        <el-button type="primary" @click="btnOk">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetail } from '@/api/user'
import { setUserRole } from '@/api/employee'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      roleList: [],
      checkList: []
    }
  },
  created() {
    this.getRoleListFn()
  },
  methods: {
    //   关闭弹窗
    closeFn() {
      this.checkList = []
      this.$emit('update:showRoleDialog', false)
    },
    // 获取角色列表
    async getRoleListFn() {
      const { rows } = await getRoleList()
      //   console.log(rows)
      this.roleList = rows
    },
    // 分配角色
    async btnOk() {
      await setUserRole({ id: this.userId, roleIds: this.checkList })
      this.$message.success('分配成功')
      this.closeFn()
    },
    // 获取员工职位
    async getCheckList(id) {
      const { roleIds } = await getUserDetail(id)
      this.checkList = roleIds || []
    }
  }
}
</script>

<style>

</style>
